home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / nihcl-30.lha / nihcl-3.0 / lib / Collection.c < prev    next >
C/C++ Source or Header  |  1990-05-19  |  3KB  |  130 lines

  1. /* Collection.c -- implementation of abstract Collection class
  2.  
  3.     THIS SOFTWARE FITS THE DESCRIPTION IN THE U.S. COPYRIGHT ACT OF A
  4.     "UNITED STATES GOVERNMENT WORK".  IT WAS WRITTEN AS A PART OF THE
  5.     AUTHOR'S OFFICIAL DUTIES AS A GOVERNMENT EMPLOYEE.  THIS MEANS IT
  6.     CANNOT BE COPYRIGHTED.  THIS SOFTWARE IS FREELY AVAILABLE TO THE
  7.     PUBLIC FOR USE WITHOUT A COPYRIGHT NOTICE, AND THERE ARE NO
  8.     RESTRICTIONS ON ITS USE, NOW OR SUBSEQUENTLY.
  9.  
  10. Author:
  11.     K. E. Gorlen
  12.     Bg. 12A, Rm. 2033
  13.     Computer Systems Laboratory
  14.     Division of Computer Research and Technology
  15.     National Institutes of Health
  16.     Bethesda, Maryland 20892
  17.     Phone: (301) 496-1111
  18.     uucp: uunet!nih-csl!kgorlen
  19.     Internet: kgorlen@alw.nih.gov
  20.     September, 1985
  21.  
  22.  
  23. Function:
  24.     
  25. Collection is an abstract class that actually implements only the
  26. addAll, removeAll, includes, isEmpty, and Collection conversion
  27. functions.  Note that the functions Collection::asBag, asOrderedCltn,
  28. asSet, and asSortedCltn are defined in the file that implements the
  29. respective target Collection so that all of these classes are not loaded
  30. whenever any one Collection is used.
  31.  
  32. $Log:    Collection.c,v $
  33.  * Revision 3.0  90/05/20  00:19:18  kgorlen
  34.  * Release for 1st edition.
  35.  * 
  36. */
  37.  
  38. #include "Collection.h"
  39. #include "ArrayOb.h"
  40. #include "nihclIO.h"
  41.  
  42. #define    THIS    Collection
  43. #define    BASE    Object
  44. #define BASE_CLASSES BASE::desc()
  45. #define MEMBER_CLASSES
  46. #define VIRTUAL_BASE_CLASSES Object::desc()
  47.  
  48. DEFINE_ABSTRACT_CLASS(Collection,0,"$Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/lib/RCS/Collection.c,v 3.0 90/05/20 00:19:18 kgorlen Rel $",NULL,NULL);
  49.  
  50. const unsigned Collection::DEFAULT_CAPACITY = 16;    // default initial collection capacity 
  51. const unsigned Collection::EXPANSION_INCREMENT = 32;    // collection (OrderedCltn) expansion increment 
  52. const unsigned Collection::EXPANSION_FACTOR = 2;    // collection (Set,Bag,Dictionary) expansion factor 
  53.  
  54. Collection::Collection() {}
  55.  
  56. Collection::~Collection() {}
  57.  
  58. Collection::Collection(OIOifd& fd) : BASE(fd) {}
  59.  
  60. Collection::Collection(OIOin& strm) : BASE(strm) {}
  61.  
  62. const Collection& Collection::addAll(const Collection& c)
  63. {
  64.     c.addContentsTo(*this);
  65.     return c;
  66. }
  67.  
  68. Collection& Collection::addContentsTo(Collection& c) const
  69. {
  70.     DO(*this,Object,o) c.add(*o); OD
  71.     return c;
  72. }
  73.  
  74. const Collection& Collection::removeAll(const Collection& c)
  75. {
  76.     DO(c,Object,o) remove(*o); OD
  77.     return c;
  78. }
  79.  
  80. void Collection::deepenShallowCopy() {}
  81.  
  82. bool Collection::includes(const Object& ob) const { return occurrencesOf(ob) != 0; }
  83.  
  84. bool Collection::isEmpty() const { return size() == 0; }
  85.  
  86. ArrayOb Collection::asArrayOb() const
  87. {
  88.     ArrayOb a(size());
  89.     Object** q = &(a.elem(0));
  90.     DO(*this,Object,o) *q++ = o; OD
  91.     return a;
  92. }
  93.  
  94. void Collection::doFinish(Iterator& /*pos*/) const {}
  95.  
  96. void Collection::doReset(Iterator& pos) const
  97. {
  98.     pos.index = 0;
  99.     pos.ptr = NULL;
  100.     pos.num = 0;
  101. }
  102.  
  103. void Collection::dumpOn(ostream& strm) const
  104. {
  105.     strm << className() << '[';
  106.     DO(*this,Object,o) o->dumpOn(strm); OD
  107.     strm << "]\n";
  108. }
  109.  
  110. void Collection::printOn(ostream& strm) const
  111. {
  112.     int n = 0;
  113.     DO(*this,Object,o)
  114.         if (n++ != 0) strm << '\n';
  115.         o->printOn(strm);
  116.     OD
  117. }
  118.  
  119. void Collection::_storer(OIOout& strm) const
  120. {
  121.     strm << capacity() << size();
  122.     DO(*this,Object,o) o->storeOn(strm); OD
  123. }
  124.  
  125. void Collection::_storer(OIOofd& fd) const
  126. {
  127.     fd << capacity() << size();
  128.     DO(*this,Object,ob) ob->storeOn(fd); OD
  129. }
  130.